package seekting.demo2017.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.seekting.demo_lib.MainActivity;

import seekting.demo2017.AppEnv;

/**
 * Created by seekting on 17-5-18.
 */

public class InterruptedActivity extends Activity {

    public static final boolean DEBUG = AppEnv.bAppdebug;
    public static final String TAG = InterruptedActivity.class.getSimpleName();

    static {

        MainActivity.TITLE_MAP.put(InterruptedActivity.class, "");
        MainActivity.DESC_MAP.put(InterruptedActivity.class, "Interrupted调用之后：如果有sleep等需要catch Interrupted的代码，它会走catch interrupted,并把interrupted置为false，如果没有地方catch这个异常，它会标识interrupted为true，直到线程运行到catch的地方才会把它标回来");
    }

    private int i = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DEBUG) {
            Log.d(TAG, "onCreate.");
        }
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        Button button = new Button(this);
        button.setText("start");
        Button button1 = new Button(this);
        button1.setText("interrupt");
        linearLayout.addView(button);
        linearLayout.addView(button1);


        final Thread thread = new Thread() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
//                    Thread.sleep(1000);
                    if (DEBUG) {
                        boolean isIn = isInterrupted();
                        if (isIn) {
                            Log.d(TAG, "run." + isInterrupted());
                            i++;
                            if (i > 3) {
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }else{
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                Log.d(TAG, "run.InterruptedException" + isInterrupted());
                                e.printStackTrace();
                            }
                        }

                    }
//                    try {
//
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                }
            }
        };

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                thread.start();
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thread.interrupt();
            }
        });


        setContentView(linearLayout);
    }
}
